﻿using System.Windows;
using System;

namespace Mesuro.Graphing {
	public struct Insets {
		public double Left { get; set; }
		public double Top { get; set; }
		public double Right { get; set; }
		public double Bottom { get; set; }
		public double TotalWidth { get { return Left + Right; } }
		public double TotalHeight { get { return Top + Bottom; } }

		public Insets( double all )
			: this() {
			Left = all;
			Top = all;
			Right = all;
			Bottom = all;
		}
		public Insets( double leftRight, double topBottom )
			: this() {
			Left = leftRight;
			Top = topBottom;
			Right = leftRight;
			Bottom = topBottom;
		}
		public Insets( double left, double top, double right, double bottom )
			: this() {
			Left = left;
			Top = top;
			Right = right;
			Bottom = bottom;
		}

		public static Insets operator +( Insets a, Insets b ) {
			return new Insets( a.Left + b.Left,
				a.Top + b.Top,
				a.Right + b.Right,
				a.Bottom + b.Bottom );
		}

		public static Rect operator -( Rect rect, Insets insets ) {
			rect.X += insets.Left;
			rect.Y += insets.Top;
			rect.Width = Math.Max( rect.Width - insets.TotalWidth, 0 );
			rect.Height = Math.Max( rect.Height - insets.TotalHeight, 0 );
			return rect;
		}
		public static Rect operator +( Rect rect, Insets insets ) {
			rect.X -= insets.Left;
			rect.Y -= insets.Top;
			rect.Width += insets.TotalWidth;
			rect.Height += insets.TotalHeight;
			return rect;
		}
		public static Size operator -( Size size, Insets insets ) {
			size.Width = Math.Max( size.Width - insets.TotalWidth, 0 );
			size.Height = Math.Max( size.Height - insets.TotalHeight, 0 );
			return size;
		}
		public static Size operator +( Size size, Insets insets ) {
			size.Width += insets.TotalWidth;
			size.Height += insets.TotalHeight;
			return size;
		}

		public static implicit operator Thickness( Insets insets ) {
			return new Thickness( insets.Left, insets.Top, insets.Right, insets.Bottom );
		}
		public static implicit operator Insets( Thickness thick ) {
			return new Insets( thick.Left, thick.Top, thick.Right, thick.Bottom );
		}
	}
}
